package cn.ilikes.tools.hbase.jpa.clients;

import org.apache.hadoop.hbase.client.Result;
import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.JPAHbasePersistenUnitEnv;
import cn.ilikes.tools.hbase.jpa.clients.datahander.DataHander;
import cn.ilikes.tools.hbase.jpa.clients.datahander.JPAHBaseDataHnaderException;
import cn.ilikes.tools.hbase.jpa.clients.nodes.Node;
import cn.ilikes.tools.hbase.jpa.clients.nodes.NodeLifeCycle.LifeCycle;
import cn.ilikes.tools.hbase.jpa.indexs.IndexClient;
import cn.ilikes.tools.hbase.jpa.indexs.IndexManagerException;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.untils.HBaseUtils;

public class HbaseClient extends AbClient {
	private Logger logger = Loggerfactory.instance(HbaseClient.class);

	public HbaseClient(IndexClient indexClient, DataHander dataHander, JPAHbasePersistenUnitEnv hbasePersistenUnitEnv) {
		super(indexClient, dataHander, hbasePersistenUnitEnv);

	}

	@Override
	public <V> Node findById(Class<V> clazz, Object key) throws JPAHBaseClientsException {

		IEntityMetaData entityMetaData = findIEntityMetaData(clazz);

		DataHander dataHander = getDataHander();

		Result result = dataHander.find(entityMetaData, key);

		return this.ResultToObject(entityMetaData, result);

	}

	@Override
	public Node save(Node entity) throws JPAHBaseClientsException {
		try {
			getDataHander().save(entity);
			IndexClient indexClient = getIndexClient();
			indexClient.add(entity);
			//			entity.getNodeLifeCycle().setLifeCycle(LifeCycle.managed);
			return entity;
		} catch (IndexManagerException e) {
			throw new JPAHBaseClientsException(e);
		} catch (JPAHBaseDataHnaderException e) {
			throw new JPAHBaseClientsException(e);
		}

	}

	@Override
	public void delete(Node node) throws JPAHBaseClientsException {
		try {
			DataHander dataHander = getDataHander();
			dataHander.remove(node);
			IndexClient indexClient = getIndexClient();
			indexClient.remove(node);
		} catch (IndexManagerException e) {
			throw new JPAHBaseClientsException(e);
		} catch (JPAHBaseDataHnaderException e) {
			throw new JPAHBaseClientsException(e);
		}
	}

	@Override
	public Node update(Node node) throws JPAHBaseClientsException {
		try {
			DataHander dataHander = getDataHander();
			dataHander.update(node);
			IndexClient indexClient = getIndexClient();
			indexClient.update(node);
			//			node.getNodeLifeCycle().setLifeCycle(LifeCycle.managed);
			return node;
		} catch (IndexManagerException e) {
			throw new JPAHBaseClientsException(e);
		} catch (JPAHBaseDataHnaderException e) {
			throw new JPAHBaseClientsException(e);
		}

	}

	@Override
	public Object findValue(IEntityMetaData entityMetaData,Object key, Class valueClazz,String family, String qualifier) throws JPAHBaseClientsException {
	
		DataHander dataHander = getDataHander();
		byte[] result=dataHander.findValue(entityMetaData, key, family, qualifier);
		
		return HBaseUtils.getByteTosObject(result, valueClazz);
		
	}

}